/*
 * Galaxium Messenger
 * 
 * Copyright (C) 2008 Philippe Durand <draekz@gmail.com>
 * 
 * License: GNU General Public License (GPL)
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

using System;
using System.Collections.Generic;

using Galaxium.Core;

namespace Galaxium.Protocol.GaduGadu
{
	public static class GaduPresence
	{
		private static IPresence _online;
		private static IPresence _offline;
		private static IPresence _away;
		private static IPresence _dnd;
		private static IPresence _busy;
		private static IPresence _idle;
		private static IPresence _chat;
		private static IPresence _invisible;

		public static IPresence Online
		{
			get
			{
				if (_online == null)
					_online = new OnlinePresence ();
				return _online;
			}
		}

		public static IPresence Offline
		{
			get
			{
				if (_offline == null)
					_offline = new OfflinePresence ();
				return _offline;
			}
		}

		public static IPresence Away
		{
			get
			{
				if (_away == null)
					_away = new AwayPresence ();
				return _away;
			}
		}

		public static IPresence Dnd
		{
			get
			{
				if (_dnd == null)
					_dnd = new DndPresence ();
				return _dnd;
			}
		}

		public static IPresence Busy
		{
			get
			{
				if (_busy == null)
					_busy = new BusyPresence ();
				return _busy;
			}
		}
		
		public static IPresence Chat
		{
			get
			{
				if (_chat == null)
					_chat = new ChatPresence ();
				return _chat;
			}
		}
		
		public static IPresence Idle
		{
			get
			{
				if (_idle == null)
					_idle = new IdlePresence ();
				return _idle;
			}
		}
		
		public static IPresence Invisible
		{
			get
			{
				if (_invisible == null)
					_invisible = new InvisiblePresence ();
				return _invisible;
			}
		}

		public static bool IsValidGaduPresence (IPresence presence)
		{
			if (presence == null)
				return false;

			return presence.Equals (_online) || presence.Equals (_offline)
				|| presence.Equals (_away) || presence.Equals (_dnd)
				|| presence.Equals (_busy) || presence.Equals (_idle)
				|| presence.Equals (_chat) || presence.Equals (_invisible);
		}

		public static bool IsValidGaduPresence (int presence)
		{
			return Int32.Equals (presence, GaduConstants.STATUS_AVAIL)
				|| Int32.Equals (presence, GaduConstants.STATUS_AVAIL_DESC)
				|| Int32.Equals (presence, GaduConstants.STATUS_BLOCKED)
				|| Int32.Equals (presence, GaduConstants.STATUS_BUSY)
				|| Int32.Equals (presence, GaduConstants.STATUS_BUSY_DESC)
				|| Int32.Equals (presence, GaduConstants.STATUS_FRIENDS_MASK)
				|| Int32.Equals (presence, GaduConstants.STATUS_INVISIBLE)
				|| Int32.Equals (presence, GaduConstants.STATUS_INVISIBLE_DESC)
				|| Int32.Equals (presence, GaduConstants.STATUS_NOT_AVAIL)
				|| Int32.Equals (presence, GaduConstants.STATUS_NOT_AVAIL_DESC);
		}

		public static IPresence ParseGaduPresence (int presence)
		{
			if (Int32.Equals (presence, GaduConstants.STATUS_AVAIL)) {
				return Online;
			} else if (Int32.Equals (presence, GaduConstants.STATUS_AVAIL_DESC)) {
				return Online;
			} else if (Int32.Equals (presence, GaduConstants.STATUS_BLOCKED)) {
				return Offline;
			} else if (Int32.Equals (presence, GaduConstants.STATUS_BUSY)) {
				return Busy;
			} else if (Int32.Equals (presence, GaduConstants.STATUS_BUSY_DESC)) {
				return Busy;
			} else if (Int32.Equals (presence, GaduConstants.STATUS_FRIENDS_MASK)) {
				return Online;
			} else if (Int32.Equals (presence, GaduConstants.STATUS_INVISIBLE)) {
				return Invisible;
			} else if (Int32.Equals (presence, GaduConstants.STATUS_INVISIBLE_DESC)) {
				return Invisible;
			} else if (Int32.Equals (presence, GaduConstants.STATUS_NOT_AVAIL)) {
				return Dnd;
			} else if (Int32.Equals (presence, GaduConstants.STATUS_NOT_AVAIL_DESC)) {
				return Dnd;
			}

			return UnknownPresence.Instance;
		}

		public static string GetIconString (IPresence presence)
		{
			//TODO: fix the icon names a bit
			if (presence.Equals (GaduPresence.Online))
				return "galaxium-status-online";
			else if (presence.Equals (GaduPresence.Chat))
				return "galaxium-status-online";
			else if (presence.Equals (GaduPresence.Away))
				return "galaxium-status-away";
			else if (presence.Equals (GaduPresence.Busy))
				return "galaxium-status-busy";
			else if (presence.Equals (GaduPresence.Dnd))
				return "galaxium-status-busy";
			else if (presence.Equals (GaduPresence.Idle))
				return "galaxium-status-idle";
			else
				return "galaxium-status-offline";
		}

		public static int GetGaduPresenceCode (IPresence presence)
		{
			if (presence.Equals (GaduPresence.Online))
				return GaduConstants.STATUS_AVAIL;
			else if (presence.Equals (GaduPresence.Away))
				return GaduConstants.STATUS_NOT_AVAIL;
			else if (presence.Equals (GaduPresence.Dnd))
				return GaduConstants.STATUS_NOT_AVAIL;
			else if (presence.Equals (GaduPresence.Busy))
				return GaduConstants.STATUS_BUSY;
			else if (presence.Equals (GaduPresence.Idle))
				return GaduConstants.STATUS_NOT_AVAIL;
			else if (presence.Equals (GaduPresence.Invisible))
				return GaduConstants.STATUS_INVISIBLE;
			else
				return GaduConstants.STATUS_NOT_AVAIL;
		}
		
		public static IPresence Get(BasePresence basePresence)
		{
			if (basePresence == BasePresence.Online)
				return Online;
			if (basePresence == BasePresence.Away)
				return Away;
			if (basePresence == BasePresence.Idle)
				return Idle;
			if (basePresence == BasePresence.Invisible)
				return Invisible;
			if (basePresence == BasePresence.Offline)
				return Offline;
			
			return null;
		}
	}

	internal sealed class OnlinePresence : AbstractPresence
	{
		public OnlinePresence ()
		{
			base._basePresence = BasePresence.Online; 
		}
		
		public override string Icon
		{
			get { return "galaxium-tray-online"; }
		}
		
		public override string State
		{
			get { return "Online"; }
		}
	}

	internal sealed class OfflinePresence : AbstractPresence
	{
		public OfflinePresence ()
		{
			_basePresence = BasePresence.Offline; 
		}
		
		public override string Icon
		{
			get { return "galaxium-tray-offline"; }
		}
		
		public override string State
		{
			get { return "Offline"; }
		}
	}

	internal sealed class BusyPresence : AbstractPresence
	{
		public BusyPresence ()
		{
			_basePresence = BasePresence.Away; 
		}
		
		public override string Icon
		{
			get { return "galaxium-tray-busy"; }
		}
		
		public override string State
		{
			get { return "Busy"; }
		}
	}

	internal sealed class DndPresence : AbstractPresence
	{
		public DndPresence ()
		{
			_basePresence = BasePresence.Away; 
		}
		
		public override string Icon
		{
			get { return "galaxium-tray-busy"; }
		}
		
		public override string State
		{
			get { return "Do Not Disturb"; }
		}
	}

	internal sealed class IdlePresence : AbstractPresence
	{
		public IdlePresence ()
		{
			_basePresence = BasePresence.Idle; 
		}
		
		public override string Icon
		{
			get { return "galaxium-tray-idle"; }
		}
		
		public override string State
		{
			get { return "Idle"; }
		}
	}

	internal sealed class AwayPresence : AbstractPresence
	{
		public AwayPresence ()
		{
			_basePresence = BasePresence.Away; 
		}
		
		public override string Icon
		{
			get { return "galaxium-tray-away"; }
		}
		
		public override string State
		{
			get { return "Away"; }
		}
	}
	
	internal sealed class ChatPresence : AbstractPresence
	{
		public ChatPresence ()
		{
			_basePresence = BasePresence.Online;
		}
		
		public override string Icon
		{
			get { return "galaxium-tray-online"; }
		}
		
		public override string State
		{
			get { return "Free For Chat"; }
		}
	}
	
	internal sealed class InvisiblePresence : AbstractPresence
	{
		public InvisiblePresence ()
		{
			_basePresence = BasePresence.Invisible; 
		}
		
		public override string Icon
		{
			get { return "galaxium-tray-invisible"; }
		}
		
		public override string State
		{
			get { return "Invisible"; }
		}
	}
}